<template>
  <view class="list">
    <navbar/>
    <u-swipe-action>
      <u-swipe-action-item
        v-for="(item, index) in list" :key="index" :options="deleteOption"
        @click="handleDelete(item)">
        <u-cell>
          <template #title>
            <view @click="handleClick(item)">
              <view>标题:{{ item.title }}</view>
              <view>用户:{{ item.user.realname }}</view>
              <view>部门:{{ item.organization.name }}</view>
              <view>事件时间:{{ parseTime(item.date, '{y}-{m}-{d}') }}</view>
            </view>
          </template>
        </u-cell>
      </u-swipe-action-item>
    </u-swipe-action>
    <u-loadmore v-if="finished" loadmore-text="我也是有底线的" dashed line/>
    <u-button class="add-button" type="primary" shape="circle" icon="plus" @click="handleCreate"/>
  </view>
</template>

<script>
import { deleteEvent, fetchEventList } from '@/api/event/event'

export default {
  name: 'EventList',
  data() {
    return {
      list: [],
      finished: false,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSizes
      }
    }
  },
  onLoad() {
    this.getList()
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.finished = false
    this.listQuery.page = 1
    this.list = []
    this.getList()
  },
  // 上拉加载
  onReachBottom() {
    if (this.finished) {
      return
    }
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      fetchEventList(this.listQuery).then(response => {
        uni.stopPullDownRefresh()
        if (response.data.success) {
          this.list = this.list.concat(response.data.data)
          this.listQuery.page++
          this.finished = response.data.data.length < this.listQuery.limit
        }
      })
    },
    // 新增
    handleCreate() {
      uni.navigateTo({
        url: '/pages/event/eventForm'
      })
    },
    // 列表数据点击事件
    handleClick(item) {
      uni.navigateTo({
        url: `/pages/event/eventDetail?id=${item.id}&name=${item.user.realname}&department=${item.organization.name}&time=${item.date}`
      })
    },
    // 删除列表数据
    handleDelete(row) {
      this.$uni.showModal('提示', '您确定删除该数据吗？').then(res => {
        if (res.confirm) {
          deleteEvent(row.id).then(response => {
            if (response.data.success) {
              this.list.splice(this.list.indexOf(row), 1)
              this.$uni.showToast(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
